
* Set directories

cd /Users/xiaosongw/Dropbox/Research/InformedSources/ReplicationRemove2


* --------------------- Figure 4: Restoration Timing  ------------------------*

* Load data
use ./Data/is_mel_p_2005_2019, clear

* Sample Period
keep if t>=td(1jan2014) & t<=td(31dec2018)


drop if drop_coles == 1
count if !missing(p)

forval j = 1/7 {
	gen pre_coles_`j' = (1-coles_off)*cond(bid==3 & cyc_day_bttm==`j',1,0)
	gen pos_coles_`j' = coles_off*cond(bid==3 & cyc_day_bttm==`j',1,0)
	
	gen pre_rival_`j' = (1-coles_off)*cond((bid==1|bid==2|bid==4|bid==5) & cyc_day_bttm==`j',1,0)
	gen pos_rival_`j' = coles_off*cond((bid==1|bid==2|bid==4|bid==5) & cyc_day_bttm==`j',1,0)
}

local cyc_id_case1 = 28
local cyc_id_case2 = 29

keep if is_cyc_drop == 0 & drop_coles == 0 & cyc_id_bttm > 0 /// 
	& cyc_id_bttm != `cyc_id_case1' & cyc_id_bttm != `cyc_id_case2'
* drop station-level cycles with length less than 10 days or greater than 130 days 
* drop coles stations with no manual price collection after the exit
* drop the first incomplete market cycle
* drop cycle 28 and 29 during the transition period
 

regress st_peak pre_coles_* pos_coles_* pre_rival_* pos_rival_*, vce(cluster id t)
test pre_coles_1 = pos_coles_1
test pre_rival_2 = pos_rival_2
estimates store full

parmest, saving(./Temp/coef_plot_data, replace) level(95)

use ./Temp/coef_plot_data, clear

* Optional: keep only relevant coefficients
keep if strpos(parm, "pre_") | strpos(parm, "pos_")

* Extract day from variable name (assuming format like "pre_coles_1")
gen day = real(regexs(1)) if regexm(parm, "_([1-7])$")
* Tag group for plotting
gen gid = ""
replace gid = "Coles" if strpos(parm, "_coles")
replace gid = "Rival" if strpos(parm, "_rival")
gen group = ""
replace group = "Pre"   if strpos(parm, "pre_")
replace group = "Post"  if strpos(parm, "pos_")

gen offset = .
replace offset = -0.15 if group == "Pre"
replace offset = +0.15 if group == "Post"
gen x = day+offset


twoway ///
    (bar estimate x if gid == "Coles" & group == "Pre", barwidth(0.22) color(red%40) lcolor(black)) ///
    (bar estimate x if gid == "Coles" & group == "Post", barwidth(0.22) color(red%80) lcolor(black)) ///
    (rcap min95 max95 x if gid == "Coles" & group == "Pre", color(red) lcolor(red))  ///
	(rcap min95 max95 x if gid == "Coles" & group == "Post", color(red) lcolor(red)  ///
    xlabel(1(1)7, nogrid) ///
	xscale(range(0.5 7.5)) ///
	yscale(range(-0.01 0.36)) ylabel(0(0.1)0.35, grid) ///
    xtitle("Day of Restoration") ///
    ytitle("Proportion of Stations Restoring Prices") ///
    legend(order(1 "Coles {bf:ON}" "Informed Sources" 2 "Coles {bf:OFF}" "Informed Sources") rows(2) position(11) size(*0.6) symx(*0.6) ring(0) region(lwidth(*0.5))) ///
	plotregion(margin(zero) style(none) fcolor(white)) graphregion(color(white)) bgcolor(white) ///
    yline(0, lpattern(dash)) ysize(5) xsize(5))
graph export ./Output/fig4a_timing_coles.pdf, as(pdf) replace

twoway ///
    (bar estimate x if gid == "Rival" & group == "Pre", barwidth(0.22) color(gs0%40) lcolor(black)) ///
    (bar estimate x if gid == "Rival" & group == "Post", barwidth(0.22) color(gs0%80) lcolor(black)) ///
    (rcap min95 max95 x if gid == "Rival" & group == "Pre", color(black) lcolor(black))  ///
	(rcap min95 max95 x if gid == "Rival" & group == "Post", color(black) lcolor(black)  ///
    xlabel(1(1)7, nogrid) ///
	xscale(range(0.5 7.5)) ///
	yscale(range(-0.01 0.36)) ylabel(0(0.1)0.35, grid) ///
    xtitle("Day of Restoration") ///
    ytitle("Proportion of Stations Restoring Prices") ///
	legend(off) ///
	plotregion(margin(zero) style(none) fcolor(white)) graphregion(color(white)) bgcolor(white) ///
    yline(0, lpattern(dash)) ysize(5) xsize(5)) 
graph export ./Output/fig4b_timing_riv.pdf, as(pdf) replace
